package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.ProjectBreifService;
import com.woniu.p2p.entity.*;
import com.woniu.p2p.listener.ProjectbreifListener;
import com.woniu.p2p.vo.AllInvestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("/projectbreif")
public class ProjectBreifController {

    @Autowired
    ProjectBreifService projectBreifService;

    @RequestMapping("/getallprojectbreif")
    public String getAllprojectbreif(Map map, String investstatus,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                                     String investkeyword,
                                     Integer pagenum,
                                     Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        pagesize=pagesize==null?3:pagesize;
        PageInfo<AllInvestVo> pageInfo = projectBreifService.findAllInvest(pagenum, pagesize, investstatus, predate, nextdate, investkeyword);
        map.put("allinvest",pageInfo);
        map.put("investstatus",investstatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("investkeyword",investkeyword);
        map.put("pagesize",pagesize);

        List<InvestRepay> allInvestRepay = projectBreifService.findAllInvestRepay();
        map.put("insertRepay",allInvestRepay);

        List<InvestStafguard> allStaf = projectBreifService.findAllStaf();
        map.put("insertstaf",allStaf);

        List<InvestCompany> allCompany = projectBreifService.findAllCompany();
        map.put("insertCompany",allCompany);

        List<InvestRisk> allRisk = projectBreifService.findAllRisk();
        map.put("insertRisk",allRisk);

        List<InvestStatus> allStatus = projectBreifService.findAllStatus();
        map.put("insertStatus",allStatus);

        List<InvestType> allType = projectBreifService.findAllType();
        map.put("investType",allType);

        List<AllInvestVo> list = projectBreifService.findall();
        ArrayList<Integer> ids = new ArrayList<>();
        for(AllInvestVo allInvestVo :list){
            ids.add(allInvestVo.getInvestId());
        }
        map.put("ids",ids);
        return "content/projectbreif";
    }

    /**
     * 导出
     */
    @RequestMapping("/projectbreifderive")
    public void exportRepository(int[] ids,
                                 HttpServletResponse response) throws IOException {
        System.out.println(Arrays.toString(ids));
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");

        List<AllInvestVo> list = projectBreifService.findAllInvest2(ids);
        System.out.println(list);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, AllInvestVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }

    @Autowired
    ProjectbreifListener projectbreifListener;

    /**
     * 导入仓库
     */
    @RequestMapping("/importProjectbreif")
    @ResponseBody
    public Integer importProjectbreif(@RequestParam("file") MultipartFile file){
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), AllInvestVo.class, projectbreifListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();

        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    //删除单个
    @RequestMapping("/deloneprojectbreif")
    public String deloneprojectbreif(Integer id){
        projectBreifService.deleteInvestById(id);
        return "content/projectbreif";
    }

    //批量删除
    @RequestMapping("/delselectedprojectbreif")
    public String delselectedprojectbreif(int[] id){
        for (int i = 1; i <=id.length ; i++) {
            projectBreifService.deleteInvestById(id[i]);
        }
        return "content/projectbreif";
    }

    //增加项目
    @RequestMapping("/insertProjectBreif")
    public int findAllRepay(Map map,String investName,long investStartPay,int investDay,long investAmount,String investDeadline,
                               Date pretime,Date nexttime,String investstafguard,String day,Integer insertRepay,Integer insertstaf,
                               Integer insertCompany,Integer insertRisk,Integer insertStatus,Integer investType){
        //编号
        long time = new Date().getTime();
        String s=time+"";
        String result = s.substring(s.length()-4,s.length());
        int i1 = Integer.parseInt(result);
        //获得年化益
        int i = Integer.parseInt(day);
        Double getyearmoney=(((investAmount*0.6)/investAmount)/i)*365*100;
        //投资余额
        long balance=0;
        //投资进度
        String subsribe="0";
        //每万元获得的收益
        long getmoneyforyear= (long) (10000*getyearmoney);
        Invest invest = new Invest(i1,investName,investStartPay,getyearmoney,investDay,investAmount,investDeadline,pretime,
                nexttime,investstafguard,balance,subsribe,day,getmoneyforyear,insertRepay,insertstaf,insertCompany,insertRisk,
                insertStatus,investType);
        int i2 = projectBreifService.insertInvest(invest);
        return 1;
    }



    //查询公司详情
    @RequestMapping("/getcomdetail")
    @ResponseBody
    public InvestCompany getcomdetail(int id){
        InvestCompany allCompanyDetails = projectBreifService.findAllCompanyDetails(id);
        return allCompanyDetails;
    }

}
